package com.example.loginsystem.service.impl;

import com.example.loginsystem.bean.Message;
import com.example.loginsystem.bean.MyUser;
import com.example.loginsystem.dao.MessageDAO;
import com.example.loginsystem.model.Paging;
import com.example.loginsystem.model.Result;
import com.example.loginsystem.service.MessageService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.lang.invoke.CallSite;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author Cheung0
 * Created on 2021/11/24
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageDAO messageDAO;

    @Override
    public Result<Message> add(Message message) {
        Result result = new Result();
        if (Objects.equals(message.getPic(), "") || Objects.equals(message.getContactInfo(), "") || Objects.equals(message.getDescription(), "")) {
            result.setResultFailed("相关信息不可为空！");
            return result;
        }
        if (messageDAO.addMessage(message) != 1) {
            result.setResultFailed("添加失败！");
            return result;
        }
        result.setResultSuccess("添加成功！", message);
        return result;
    }

    @Override
    public Result<Message> delete(int id) {
        messageDAO.deleteMessage(id);
        Result result = new Result();
        result.setResultSuccess("删除成功！");
        return result;
    }

    @Override
    public Result<Message> update(Message message) {
        messageDAO.updateMessage(message);
        Result result = new Result();
        result.setResultSuccess("更新成功！");
        return result;
    }

    @Override
    public Result<Message> getById(int id) {
        Message message = new Message();
        message = messageDAO.getMessageById(id);
        Result result = new Result();
        result.setResultSuccess("查询成功！", message);
        return result;
    }

    @Override
    public Result<List<Message>> getByUserId(String userId) {
        List<Message> messageList = new ArrayList<>();
        messageList = messageDAO.getMessageByUserId(userId);
        Result result = new Result();
        result.setResultSuccess("查询成功！", messageList);
        return result;
    }

    @Override
    public List<Message> getAll() {
        return messageDAO.getAll();
    }

    @Override
    public Paging<Message> getByPage(int current, int size) {
        Page<Message> messagePage = PageHelper.startPage(current, size).doSelectPage(() -> messageDAO.getAll());
        Paging<Message> messagePaging = new Paging<>(messagePage);
        return messagePaging;
    }

}
